//
//  KKDiscoverSearchView.swift
//  KKWeibo
//
//  Created by kassia on 16/7/13.
//  Copyright © 2016年 kassia. All rights reserved.
//

import UIKit

class KKDiscoverSearchView: UIView, UITextFieldDelegate {

    //搜索框
    @IBOutlet weak var searchText: UITextField!

    //取消button
    @IBOutlet weak var cancelButton: UIButton!

    
    @IBOutlet weak var searchRightConstraint: NSLayoutConstraint!
    
    
    //加载xib
    class func searchView()-> KKDiscoverSearchView {
    
       return UINib(nibName: "KKDiscoverSearchView", bundle: nil).instantiateWithOwner(nil, options: nil).last as! KKDiscoverSearchView
    }
    
    @IBAction func cancelButtonAction(sender: AnyObject) {
        
        searchText.resignFirstResponder()
        searchRightConstraint.constant = 0
        
        UIView.animateWithDuration(0.25) { () -> Void in
            //重新布局约束
            self.layoutIfNeeded()
        }
        
    }
    
    func textFieldDidBeginEditing(textField: UITextField) {
        //搜索
        searchRightConstraint.constant = cancelButton.weight
        //动画执行
        UIView.animateWithDuration(0.25) { () -> Void in
            //改变他的约束
            self.layoutIfNeeded()
        }
    }
}
